﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using CuaHangLaptop.BIZ;

namespace CuaHangLaptop.DAL
{
    class Kho_CTPN_DAL
    {
        internal static string Get_CTKho_ByCTPN(string _mactpn)
        {
            string _ctkho = "";
            SqlConnection conn = Connection.Connection_String();
            SqlDataReader rdr = null;
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("select MaCTKho from Kho_CTPN where MaCTPhieuNhap='"+_mactpn+"'", conn);
                rdr = cmd.ExecuteReader();
                rdr.Read();
                _ctkho = rdr.GetString(0);
            }
            catch
            {
            }
            finally
            {
                if (rdr != null) rdr.Close();
                conn.Close();
            }
            return _ctkho;
        }

        internal static void Insert(string _maCTPN, string _maCTKho)
        {
            SqlDataReader rdr = null;
            SqlConnection conn = Connection.Connection_String();
            try
            {
                conn.Open();
                String s = String.Format("insert into Kho_CTPN values('{0}', '{1}')", _maCTPN, _maCTKho);
                SqlCommand cmd = new SqlCommand(s, conn);

                if (cmd.ExecuteNonQuery() > 0)
                {
                }
            }
            finally
            {
                if (rdr != null) rdr.Close();
                conn.Close();
            }
        }

        internal static void Update(string _maCTPN, string _maCTKho)
        {
            SqlConnection conn = Connection.Connection_String();
            try
            {
                conn.Open();
                String s = String.Format("update Kho_CTPN set MaCTKho='{0}' where MaCTPhieuNhap='{1}'", _maCTKho, _maCTPN);
                SqlCommand cmd = new SqlCommand(s, conn);

                if (cmd.ExecuteNonQuery() > 0)
                {
                }
            }
            finally
            {
                conn.Close();
            }
        }

        internal static void Delete(string _mactPN)
        {
            SqlConnection conn = Connection.Connection_String();
            try
            {
                conn.Open();
                String s = String.Format("delete from Kho_CTPN where MaCTPhieuNhap='{0}'", _mactPN);
                SqlCommand cmd = new SqlCommand(s, conn);

                if (cmd.ExecuteNonQuery() > 0)
                {
                }
            }
            finally
            {
                conn.Close();
            }
        }

    }
}
